﻿@using System.ComponentModel
@using AntDesign.TableModels
@using AntDesign.Core.Extensions

<Table TItem="Data" DataSource="@data" AutoColIndexes="false">

    <GenerateColumns Range="1.." HideColumnsByName="@(new []{ "IdCard" })" Definitions=definitions />

    <ActionColumn ColIndex="4" Title="Action">
        <Space Size=@("middle")>
            <SpaceItem>
                <a>Invite @context.Name</a>
            </SpaceItem>
            <SpaceItem>
                <a>Delete</a>
            </SpaceItem>
        </Space>
    </ActionColumn>
</Table>

@code {
    Data[] data = new Data[]
    {
        new()
        {
            Key = "1",
            Name = "John Brown",
            Age = 32,
            Address = "New York No. 1 Lake Park",
            Tags = new[] {"nice", "developer"},
            IdCard = ""
        },
        new()
        {
            Key = "2",
            Name = "Jim Green",
            Age = 42,
            Address = "London No. 1 Lake Park",
            Tags = new[] { "loser"},
            IdCard = ""
        },
        new()
        {
            Key = "3",
            Name = "Joe Black",
            Age = 32,
            Address = "Sidney No. 1 Lake Park",
            Tags = new[] { "cool", "teacher" },
            IdCard = ""
        }
    };

    public class Data
    {
        [DisplayName("Key")]
        public string Key { get; set; }

        [DisplayName("Name")]
        public string Name { get; set; }

        [DisplayName("Age")]
        public int Age { get; set; }

        [DisplayName("Address")]
        public string Address { get; set; }

        [DisplayName("Tags")]
        public string[] Tags { get; set; }
        
        [DisplayName("IdCard")]
        public string IdCard { get; set; }
    }

    /// <summary>
    /// An Action to defined each column
    /// </summary>
    /// <param name="propertyName">The name of the property binding the column. </param>
    /// <param name="column">The column instance, you need to explicitly cast to a concrete Column type. </param>
    void definitions(string propertyName, IFieldColumn column)
    {
        if (propertyName == "Tags")
        {
            var tagCol = ((Column<string[]>)column);

            tagCol.CellRender = cellData =>
            @<span>
                @foreach (var tag in cellData.FieldValue)
                {
                    var color = tag.Length > 5 ? TagColor.GeekBlue : TagColor.Green;
                    if (tag == "loser")
                    {
                        color = TagColor.Volcano;
                    }
                    <Tag Color="@color">@tag</Tag>
                }
            </span>
    ;

            return;
        }

        column.Sortable = true;
        column.Filterable = true;
    }
}